﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DouEventPanel : EventPanel
{
    public CanvasGroup cg;
    public CanvasGroup hand1, hand2;
    public Text optionText1, optionText2;
    float gap;//分割操作区域
    int handStatus;//0,1;

    public override void OnEnter()
    {
        cg.blocksRaycasts = true;
        cg.interactable = true;
        cg.alpha = 1;
        activated = true;
        hand1.alpha = 0;
        hand2.alpha = 0;
        optionText1.text = optionNames[0];
        optionText2.text = optionNames[1];
        GameManager.Instance.ChangeState(StateOP.Push, GameState.Event);
        Texture2D tex = Resources.Load(imagePath) as Texture2D;
        image.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
        Input.ResetInputAxes();
    }

    public override void OnExit()
    {
        cg.blocksRaycasts = false;
        cg.interactable = false;
        cg.alpha = 0;
        activated = false;
        GameManager.Instance.ChangeState(StateOP.Pop);
    }

    public override void OnPause()
    {
        cg.blocksRaycasts = false;
        cg.interactable = false;
        cg.alpha = 0;
        activated = false;
    }

    public override void OnResume()
    {
        cg.blocksRaycasts = true;
        cg.interactable = true;
        cg.alpha = 1;
        activated = true;
        hand1.alpha = 0;
        hand2.alpha = 0;
        GameManager.Instance.ChangeState(StateOP.Push, GameState.Event);
        Input.ResetInputAxes();
    }

    void Start()
    {
        gap = (optionText1.transform.position.y + optionText2.transform.position.y) / 2;
        handStatus = 0;
    }

    void Update()
    {
        if (activated)
        {
            if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButton(0))
            {
                gotoOption(handStatus);
            }
            if (Tag.mouseDelay > 0.5)
            {
                if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
                {
                    handStatus += 1;
                    if (handStatus > 2)
                        handStatus -= 2;
                }
                if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
                {
                    handStatus -= 1;
                    if (handStatus < 0)
                        handStatus += 2;
                }

            }
            else
            {
                MousePos = Input.mousePosition;
                if (MousePos.y < gap)
                {
                    handStatus = 0;
                }
                else
                {
                    handStatus = 1;
                }
            }
            switch (handStatus)
            {
                case 0:
                    hand1.alpha = 1;
                    hand2.alpha = 0;
                    break;
                case 1:
                    hand1.alpha = 0;
                    hand2.alpha = 1;
                    break;
            }
        }
    }
}
